package cn.tedu.sp04.service;

import cn.tedu.sp01.pojo.Item;
import cn.tedu.sp04.feign.ItemClient;
import cn.tedu.sp04.feign.UserClient;
import cn.tedu.web.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tedu.sp01.pojo.Order;
import cn.tedu.sp01.service.OrderService;

import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Usefulness: 用途
 * @Author: LeiTing
 * @Date: 2021/10/18  15:52
 * @Version 1.0
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private UserClient userClient;

    @Override
    public Order getOrder(String orderId) {
        log.info("获取订单，orderId= "+orderId);
        //远程调用商品，获取商品列表
        JsonResult<List<Item>> items = itemClient.getItems(orderId);
        //远程调用用户，获取用户数据
        userClient.getUser(8);
        //TODO: 调用user-service获取用户信息
        //TODO: 调用item-service获取商品信息
        Order order = new Order();
        order.setId(orderId);
        return order;
    }

    @Override
    public void addOrder(Order order) {
        log.info("添加订单： "+order);
        //远程调用商品，减少库存
        itemClient.decreaseNumber(order.getItems());
        //远程调用用户，增加积分
        userClient.addScore(order.getUser().getId(),1000);
        //TODO: 调用item-service减少商品库存
        //TODO: 调用user-service增加用户积分
        log.info("保存订单："+order);
    }

}

